<?php

namespace frontend\modules\api\controllers;

use Yii;
use yii\web\Controller;
use common\models\LoginForm;

/**
 * Default controller for the `forum` module
 */
class DashouController extends Controller
{
    public $layout = 'main_api';
    public $enableCsrfValidation = false;

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        $req_json = file_get_contents("php://input");
        $req_obj = null;
        if($req_json) {
            $req_obj = json_decode($req_json);
        }

        $resp = "";
        if($req_obj) {
            $resp = $this->handleRequest($req_obj);
        }

        return $resp;
    }

    public function actionPost() {
        return $this->render('post');
    }

    public function actionAjaxpost() {
        $post_data = Yii::$app->request->post();
        $req_data = $post_data['data'];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://www.yiidev.com/api/dashou");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch, CURLOPT_POSTFIELDS, $req_data);
        $output = curl_exec($ch);
        curl_close($ch);

        return $output;
    }

    private function makeRequest() {

    }

    private function handleRequest($req) {
        $result_obj = null;
        if(0 == strcmp("getsession", $req->action)) {
            $result_obj = $this->handleRequestForGetSession($req);
        } else if(0 == strcmp("userlogin", $req->action)) {
            $result_obj = $this->handleRequestForUserLogin($req);
        } else if(0 == strcmp("getuserinfo", $req->action)) {
            $result_obj = $this->handleRequestForGetUserInfo($req);
        }

        if($result_obj != null) {
            return (json_encode($result_obj));
        }
        return '';
    }

    private function handleRequestForGetSession($req) {
        $session = Yii::$app->session;
        $session->open();
        $result_obj = new \stdClass;
        $result_obj->action = $req->action;
        $result_obj->session = $session->getId();
        $result_obj->code = 0;
        $result_obj->msg = 'sucess';
        return $result_obj;
    }

    private function handleRequestForUserLogin($req) {
        $result_obj = new \stdClass;
        $result_obj->action = $req->action;

        if(strlen($req->session) > 0) {
            session_id($req->session);
            Yii::$app->getSession()->open();

            $result_obj->session = $req->session;
            $result_obj->code = 0;
            $result_obj->msg = 'sucess';

            $model = new LoginForm();
            $model->username = $req->username;
            $model->password = $req->password;
            if ($model->login()) {
            } else {
                $result_obj->code = 1;
                $result_obj->msg = '用户名或密码不正确';
            }
        } else {
            $result_obj->code = 1;
            $result_obj->msg = '用户名或密码不正确';
        }
        return $result_obj;
    }

    private function handleRequestForGetUserInfo($req) {
        $result_obj = new \stdClass;
        $result_obj->action = $req->action;

        if(strlen($req->session) > 0) {
            session_id($req->session);
            Yii::$app->getSession()->open();

            $result_obj->session = $req->session;
            $result_obj->code = 1;
            $result_obj->msg = 'failed';

            $user = Yii::$app->user;
            if(!$user->isGuest) {
                $username = $user->identity->username;
                $result_obj->username = $username;
                $result_obj->code = 0;
                $result_obj->msg = 'sucess';
            } else {
                $result_obj->code = 2;
                $result_obj->msg = 'is guest';
            }
        }
        return $result_obj;
    }
}
